//账号申诉
function Appeal() {
    for (let i = 0; i < 5; i++) {
        if (Send_bot()) {
            return true;
        }
        var back = desc("Go back");
        click(back);
        logd("返回至Telegram主界面");
        sleep(1000);
    }
    return false;
}

function input_text(text_str) {
    var key = null;
    var change_1 = null;
    var change_2 = null;
    for (let i = 0; i < text_str.length; i++) {
        if (Number.isFinite(text_str[i] % 65535) || text_str[i] === '@' || text_str[i] === '/') {
            change_1 = desc('Symbol keyboard').getOneNodeInfo(500);
            if (change_1) {
                change_1.click();
                sleep(500);
                if (text_str[i] === '/') {
                    var a = desc('forward slash', pkg('com.google.android.inputmethod.latin')).getOneNodeInfo(400);
                    if (a) {
                        a.click();
                    }
                }

            }
        } else {
            change_2 = desc('Letter keyboard').getOneNodeInfo(400);
            if (change_2) {
                change_2.click();
            }
        }
        key = desc(text_str[i], pkg('com.google.android.inputmethod.latin')).getOneNodeInfo(400);
        if (key) {
            key.click();
        }
        sleep(500);
    }
    return true;
}

function Send_bot() {
    var search1 = desc("Search");
    var result1 = has(search1);
    if (result1) {
        click(search1);
        logd("点击搜索按钮");
        sleep(1000);

        var search2 = text("Search");
        var result2 = has(search2);
        if (result2) {
            click(search2);
            sleep(1000);
            input_text("@Spambot");
            logd("输入:@Spambot");
            sleep(1500);

            var title = textMatch("Spam Info Bot.*");
            var result3 = waitExistNode(title, 10000);
            if (result3) {
                click(title);
                logd("与机器人对话...");
                sleep(1500);

                var spam = text("Spam Info Bot");
                if (has(spam)) {
                    var state = false;
                    var start = text("START");
                    if (has(start)) {
                        click(start);
                        logd("点击START");
                        sleep(1500);
                        state = true;
                    } else {
                        var message = text("Message");
                        var result4 = has(message);
                        if (result4) {
                            click(message);
                            sleep(1000);
                            input_text("/start");
                            sleep(1000);
                            click(desc("Send"));
                            logd("没有START按钮,输入:/start");
                            state = true;
                            sleep(1500);
                        } else {
                            toast("没有START按钮,发送信息失败");
                        }
                    }
                    if (state) {
                        var submit = text("Submit a complaint");
                        if (has(submit)) {
                            sleep(1000);
                            click(submit);
                            logd("点击Submit a complaint");
                            sleep(1500);

                            for (let i = 0; i < 5; i++) {
                                var button = text("No, I’ll never do any of this!");
                                if (has(button)) {
                                    click(button);
                                    logd("点击No，我没有");
                                    sleep(1500);
                                }
                            }
                        } else {
                            var cool = text("Cool, thanks");
                            if (has(cool)) {
                                click(cool);
                                logd("账号已无任何异常");
                                sleep(1000);
                            }
                        }
                        var to_back = desc("Go back");
                        click(to_back);
                        logd("返回至Telegram主界面");
                        sleep(1000);
                        return true;
                    }
                } else {
                    toast("寻找机器人错误");
                }
            } else {
                toast("没有找到机器人");
            }

        } else {
            toast("没有找到搜索输入框");
        }
    } else {
        toast("没有找到搜索按钮");
    }
}

// Appeal();